package com.blb.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.blb.entity.*;
import com.blb.entity.page.BlbPage;
import com.blb.entity.page.DataTablePage;
import com.blb.service.*;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-14
 */
@Controller
@RequestMapping("/cargo")
public class CargoController {

    @Resource
    private ICargoService cargoService;
    @Resource
    private IShelvesService shelvesService;
    @Resource
    private ICargoAreaService iCargoAreaService;
    @Resource
    private IWareService iWareService;
    @Resource
    private ICategoriesService categoriesService;

    @RequiresPermissions("cargo_manage")
    @RequestMapping("allCargos")
    public String allCargo(Model model){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        System.out.println(companyId);
        model.addAttribute("companyId",companyId);
        return "warehouse/cargoID_manage";
    }
    /*模糊查询-分页*/
    @ResponseBody
    @RequestMapping("findByLike")
    public BlbPage getBy(@RequestParam(required = false,value = "nodeId")  String nodeId,@RequestParam(required = false,value = "nodeType")  String nodeType,
                         @RequestParam(required = false,value = "companyId") String companyId,
                         @RequestParam(required = false , value = "cargoNo") String cargoNo,@RequestParam(required = false,value = "shelvesNo") String shelvesNo,
                         @RequestParam(required = false,value = "status") String status, int start, int length){
        System.out.println(companyId);
        System.out.println(nodeId);
        System.out.println(nodeType);
        //分页数据
        List<Cargo> all = cargoService.AllCargo(nodeId,nodeType,companyId,cargoNo,shelvesNo,status,start,length);
        //总记录数
        int count = cargoService.count(nodeId,nodeType,companyId,cargoNo,shelvesNo,status);
        return new DataTablePage(count,count, all);
    }
    @ResponseBody
    @RequestMapping("findMaxNum")
    public ResponseHelp findMaxNum(String shelvesId){
        Shelves shelves = shelvesService.findShelvesById(shelvesId);
        Integer dn = shelves.getTotalNum();
        int count = cargoService.count(new QueryWrapper<Cargo>().eq("shelves_id",shelvesId));
        ArrayList<Integer> list = new ArrayList<>();
        list.add(dn);
        list.add(count);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(list);
        return responseHelp;
    }
    @PostMapping("addCargo")
    public String addCargo(@RequestBody Cargo cargo){
        User user = ShiroUtils.getUser();
        cargo.setCompanyId(user.getCompanyId());
        cargo.setCreatTime(new Date());
        String i = String.valueOf(cargoService.count() + 1);
        cargo.setId("c"+i);
        cargo.setStatus("无货");
        cargoService.save(cargo);
        return "warehouse/cargoID_manage";
    }
    @PostMapping("updateCargo")
    public String updateCargo(@RequestBody Cargo cargo){
        cargoService.updateCargo(cargo);
        return "warehouse/cargoID_manage";
    }
    /*查询所有货区名*/
    @ResponseBody
    @RequestMapping("allWare")
    public ResponseHelp allWare(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        List<Ware> wares = iWareService.list(new QueryWrapper<Ware>().eq("company_id",companyId));
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(wares);
        return responseHelp;
    }
    /*查询所有货区名*/
    @ResponseBody
    @RequestMapping("allCargoArea")
    public ResponseHelp allCargoArea(String wareId){
        /*List<CargoArea> cargoAreas = iCargoAreaService.list(new QueryWrapper<CargoArea>().eq(false,"ware_id",wareId));*/
        List<CargoArea> cargoAreas = iCargoAreaService.findByWareId(wareId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(cargoAreas);
        return responseHelp;
    }
    /*货架名*/
    @ResponseBody
    @RequestMapping("allShelves")
    public ResponseHelp allShelves(String damId){
        /*List<Shelves> shelves = shelvesService.list(new QueryWrapper<Shelves>().eq(false,"dam_id",damId));*/
        List<Shelves> shelves = shelvesService.list(new QueryWrapper<Shelves>().eq("dam_id",damId));
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(shelves);
        return responseHelp;
    }
    /*货位名*/
    @ResponseBody
    @RequestMapping("allCargoByShelves")
    public ResponseHelp allCargoByShelves(String shelvesId){
        List<Cargo> cargos = cargoService.list(new QueryWrapper<Cargo>().eq("shelves_id", shelvesId).eq("status","无货"));
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(cargos);
        return responseHelp;
    }
    /*findCargoById*/
    @ResponseBody
    @RequestMapping("findCargoById")
    public ResponseHelp findCargoById(String id){
        Cargo cargo = cargoService.findCargoById(id);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(cargo);
        return responseHelp;
    }
    /**
     * updateCargoStatus
     * 更新货位状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("updateCargoStatus")
    @ResponseBody
    public ResponseHelp updateCargoStatus(String id,String status){
        cargoService.updateCargoStatus(id,status);
        ResponseHelp responseHelp = new ResponseHelp();
        return responseHelp;
    }
    @RequestMapping("findPid")
    @ResponseBody
    public List<WareMenu> findPid(){
        User user = ShiroUtils.getUser();
        String companyId = user.getCompanyId();
        System.out.println(companyId);
        List<WareMenu> menuList = cargoService.findPid(companyId);
        return menuList;
    }
    /**
     * public ResponseHelp findPid(){
     *         User user = ShiroUtils.getUser();
     *         String companyId = user.getCompanyId();
     *         System.out.println(companyId);
     *         List<WareMenu> menuList = cargoService.findPid(companyId);
     *         ResponseHelp responseHelp = new ResponseHelp();
     *         responseHelp.setData(menuList);
     *         return responseHelp;
     *     }
     */
}

